package conson.tech.manage.facade.provider;

import cn.hutool.json.JSONUtil;
import conson.tech.core.constant.SecurityConstants;
import conson.tech.core.response.Response;
import conson.tech.manage.api.coupons.form.CouponApplyForm;
import conson.tech.manage.api.coupons.form.CouponOperForm;
import conson.tech.manage.coupons.service.CouponProviderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 优惠券对外提供接口
 * Created by guoxt on 2020/7/27.
 */
@Slf4j
@RestController
@Api(value = "优惠券内部控制器", tags = "优惠券内部控制器")
@RequestMapping((SecurityConstants.INNERAPI + "/coupons"))
public class CouponProvider {


    @Autowired
    private CouponProviderService couponProviderService;

    @ApiOperation(value = "根据优惠券编码 获取用户优惠券配置信息")
    @ApiImplicitParam(name = "couponOperForm", value = "优惠券编码集合", required = true, dataType = "CouponOperForm")
    @PostMapping("/detail")
    public Response couponConfigDetails(HttpServletRequest request,
                                        @Valid @RequestBody CouponOperForm couponOperForm) {

        log.info("CouponProvider【根据优惠券编码获取优惠券配置信息】--> inParam:【{}】", JSONUtil.toJsonStr(couponOperForm));
        if (couponOperForm.getUserId() == null) {
            return Response.fail("input userId is null");
        }
        return this.couponProviderService.couponConfigDetails(couponOperForm.getUserId(), couponOperForm.getCouponsCodes());
    }

    @ApiOperation(value = "用户优惠券锁定(批量)")
    @ApiImplicitParam(name = "couponOperForm", value = "优惠券编码集合", required = true, dataType = "CouponOperForm")
    @PostMapping("/lock")
    public Response lockCoupon(HttpServletRequest request,@Valid @RequestBody CouponOperForm couponOperForm) {
        log.info("CouponProvider【用户优惠券锁定(批量)】--> inParam:【{}】", JSONUtil.toJsonStr(couponOperForm));

        return couponProviderService.lockCoupon(couponOperForm.getCouponsCodes());
    }


    @ApiOperation(value = "用户优惠券解锁(批量)")
    @ApiImplicitParam(name = "couponOperForm", value = "优惠券编码集合", required = true, dataType = "CouponOperForm")
    @PostMapping("/unlock")
    public Response unlockCoupon(HttpServletRequest request,@Valid @RequestBody CouponOperForm couponOperForm) {

        log.info("CouponProvider【用户优惠券解锁(批量)】--> inParam:【{}】", JSONUtil.toJsonStr(couponOperForm));

        return couponProviderService.unlockCoupon(couponOperForm.getCouponsCodes());
    }

    @ApiOperation(value = "用户优惠券核销(批量)")
    @ApiImplicitParam(name = "couponApplyForm", value = "优惠券编码集合", required = true, dataType = "CouponApplyForm")
    @PostMapping("/applied")
    public Response appliedCoupon(HttpServletRequest request,@Valid @RequestBody CouponApplyForm couponApplyForm) {

        log.info("CouponProvider【用户优惠券核销(批量)】--> inParam:【{}】", JSONUtil.toJsonStr(couponApplyForm));

        return couponProviderService.appliedCoupon(couponApplyForm.getCouponAppliedDetailDTOS());
    }

    @ApiOperation(value = "优惠券原路返还(单张)")
    @ApiImplicitParam(name = "couponOperForm", value = "优惠券编码集合", required = true, dataType = "CouponOperForm")
    @PostMapping("/return")
    public Response returnCoupon(HttpServletRequest request,@Valid @RequestBody CouponOperForm couponOperForm) {

        log.info("CouponProvider【优惠券原路返还(单张)】--> inParam:【{}】", JSONUtil.toJsonStr(couponOperForm));

        return couponProviderService.returnCoupon(couponOperForm.getCouponsCode());
    }


}
